1
/****************************** Module Header ******************************\
2 * Module Name: EntityConfigurations.cs
3 * Project: CSEFCodeOnly
4 * Copyright (c) Microsoft Corporation.
6 * This code file contains the EntityConfiguration for the POCO entities to
7 * create the Entity Data Model metadata.
9 * This source is subject to the Microsoft Public License.
10 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 * All other rights reserved.
13 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
14 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
16 \***************************************************************************/
18 #region Using directive
20 using System
.Collections
.Generic
;
23 using Microsoft
.Data
.Objects
;
26 namespace CSEFCodeOnly
28 #region EntityConfiguration for TPT inheritance POCO entities
29 public class PersonTPTConfiguration
: EntityConfiguration
<PersonTPT
>
31 public PersonTPTConfiguration()
34 HasKey(p
=> p
.PersonID
);
36 // Set the table mapping of the TPT inheritance entities
37 // The table name here is "PeopleTPT"
38 // Anonymous type properties are mapped to the table column names
43 Name_FirstName
= p
.Name
.FirstName
,
44 Name_LastName
= p
.Name
.LastName
,
45 Address_City
= p
.Address
.City
,
46 Address_Country
= p
.Address
.Country
,
47 Address_Zipcode
= p
.Address
.Zipcode
48 }).ToTable("PeopleTPT");
52 public class InstructorTPTConfiguration
:
53 EntityConfiguration
<InstructorTPT
>
55 public InstructorTPTConfiguration()
57 // Set the regular property column mapping
58 Property(i
=> i
.HireDate
);
60 // Set the many-to-many relationship between the Course and
61 // InstructorTPT entities
62 Relationship(i
=> i
.Courses
).FromProperty(c
=> c
.Instructors
);
64 // Set the table mapping of the TPT inheritance entities
65 // The table name here is "InstructorsTPT"
66 // Anonymous type properties are mapped to the table column names
72 }).ToTable("InstructorsTPT");
76 public class StudentTPTConfiguration
: EntityConfiguration
<StudentTPT
>
78 public StudentTPTConfiguration()
80 // Set the regular property column mappings
81 Property(s
=> s
.EnrollmentDate
);
82 Property(s
=> s
.Degree
);
83 Property(s
=> s
.Credits
);
85 // Set the table mapping of the TPT inheritance entities
86 // The table name here is "StudentsTPT"
87 // Anonymous type properties are mapped to the table column names
95 }).ToTable("StudentsTPT");
97 // Set the one-to-many relationship between the StudentTPT and
98 // CourseStudent entities
99 Relationship(s
=> s
.CourseStudents
)
100 .FromProperty(cs
=> cs
.Student
);
104 public class AdminTPTConfiguration
: EntityConfiguration
<AdminTPT
>
106 public AdminTPTConfiguration()
108 // Set the regular property column mapping
109 Property(a
=> a
.AdminDate
);
111 // Set the table mapping of the TPT inheritance entities
112 // The table name here is "AdminsTPT"
113 // Use the EntityMap.Row and EntityMap.Column to map the
114 // properties to the table columns
117 EntityMap
.Column(a
.PersonID
, "PersonID"),
118 EntityMap
.Column(a
.AdminDate
, "AdminDate")
119 )).ToTable("AdminsTPT");
123 public class BusinessStudentTPTConfiguration
:
124 EntityConfiguration
<BusinessStudentTPT
>
126 public BusinessStudentTPTConfiguration()
128 // Set the regular property column mapping
129 Property(bs
=> bs
.BusinessCredits
);
131 // Set the table mapping of the TPT inheritance entities
132 // The table name here is "BusinessStudentsTPT"
133 // Use the EntityMap.Row and EntityMap.Column to map the
134 // properties to the table columns
137 EntityMap
.Column(bs
.PersonID
, "PersonID"),
138 EntityMap
.Column(bs
.BusinessCredits
, "BusinessCredits")
139 )).ToTable("BusinessStudentsTPT");
144 #region EntityConfiguration for other relational POCO entities
145 public class DepartmentConfiguration
: EntityConfiguration
<Department
>
147 public DepartmentConfiguration()
149 // Set the entity key
150 HasKey(d
=> d
.DepartmentID
);
152 // Set the varchar typed column, max length: 100, non-nullable
153 Property(d
=> d
.Name
).HasMaxLength(100).IsRequired();
155 // Set the decimal typed column, precision: 18, scale: 0,
157 Property(d
=> d
.Budget
).Precision
= 18;
158 Property(d
=> d
.Budget
).Scale
= 0;
160 // Set the regular property column mapping
161 Property(d
=> d
.StartDate
);
163 // Set the one-to-many relationship between the Department and
165 Relationship(d
=> d
.Courses
).FromProperty(c
=> c
.Department
);
169 public class CourseConfiguration
: EntityConfiguration
<Course
>
171 public CourseConfiguration()
173 // Set the entity key
174 HasKey(c
=> c
.CourseID
);
176 // Set the varchar typed column, max length: 100, non-nullable
177 Property(c
=> c
.Title
).HasMaxLength(100).IsRequired();
179 // Set the regular property column mapping
180 Property(c
=> c
.Credits
);
182 // Set the FK association between the Department and Course
183 // entities, and set the FK association property as
184 // Course.DepartmentID
185 // IsRequired() indicates it is 1:* relationship instead of
186 // 0..1:* relationship
187 Relationship(c
=> c
.Department
).IsRequired()
188 .FromProperty(d
=> d
.Courses
).HasConstraint(
189 (c
, d
) => c
.DepartmentID
== d
.DepartmentID
);
191 // Set the one-to-many relationship between the Course and
192 // CourseStudent entities
193 Relationship(c
=> c
.CourseStudents
)
194 .FromProperty(cs
=> cs
.Course
);
196 // Set the many-to-many relationship between the Course and
197 // InstructorTPT entities
198 Relationship(c
=> c
.Instructors
)
199 .FromProperty(i
=> i
.Courses
);
203 public class CourseStudentConfiguration
:
204 EntityConfiguration
<CourseStudent
>
206 public CourseStudentConfiguration()
208 // Set the composite entity key
209 HasKey(cs
=> new { cs.PersonID, cs.CourseID }
);
211 // Set the regular property column mappings
212 Property(cs
=> cs
.PersonID
);
213 Property(cs
=> cs
.CourseID
);
214 Property(cs
=> cs
.Score
);
216 // Set the FK association between the Course and CourseStudent
217 // entities, and set the FK association property as
218 // CourseStudent.CourseID
219 Relationship(cs
=> cs
.Course
).IsRequired()
220 .FromProperty(c
=> c
.CourseStudents
)
221 .HasConstraint((cs
, c
) => cs
.CourseID
== c
.CourseID
);
223 // Set the FK association between the StudentTPT and
224 // CourseStudent entities, and set the FK association property
225 // as CourseStudent.PersonID
226 Relationship(cs
=> cs
.Student
).IsRequired()
227 .FromProperty(s
=> s
.CourseStudents
)
228 .HasConstraint((cs
, s
) => cs
.PersonID
== s
.PersonID
);
233 #region EntityConfiguration for TPH inheritance POCO entities
234 public class PersonTPHConfiguration
: EntityConfiguration
<PersonTPH
>
236 public PersonTPHConfiguration()
238 // Set the entity key
239 HasKey(p
=> p
.PersonID
);
241 // Set the table mapping of the TPT inheritance entities
242 // The table name here is "PeopleTPH"
243 // Each type has its own property and column mappings
244 // The base and each derived type have the discriminitor column
247 .Case
<PersonTPH
>(p
=> new
257 .Case
<InstructorTPH
>(i
=> new
268 .Case
<StudentTPH
>(s
=> new
279 .Case
<AdminTPH
>(a
=> new
289 }).ToTable("PeopleTPH");
294 #region ComplexTypeCinfiguration for Complex Type entities
295 public class ComplexTypeNameConfiguration
:
296 ComplexTypeConfiguration
<Name
>
298 public ComplexTypeNameConfiguration()
300 // Set the two varchar typed column the max length and
302 Property(n
=> n
.FirstName
).IsMax().IsRequired();
303 Property(n
=> n
.LastName
).IsMax().IsRequired();
307 public class ComplexTypeAddressConfiguration
:
308 ComplexTypeConfiguration
<Address
>
310 public ComplexTypeAddressConfiguration()
312 // Set the three varchar typed column the max length
313 Property(a
=> a
.City
).IsMax();
314 Property(a
=> a
.Country
).IsMax();
315 Property(a
=> a
.Zipcode
).IsMax();